import pageInfo from './pageInfo'
/* 路由跳转
 * @method router
 * @param mode {string} 请求方式 [''] 
 * @param url {string} 请求链接 
 * @param delta {Number} 后退页默认后退一页 🔙 
*/
const routerModeArr = [ // 跳转方式
  'navigateTo', 'redirectTo', 'reLaunch', 'navigateBack'
]
let option = {}
export const router = (parameter) => {
  let {data,mode,url,delta} = parameter
  // 先把值处理了在说
  option[url] = data
  
  //  然后开始跳转
  if(mode === 'navigateBack'){ // 后退
    return new Promise(function (resolve, reject) {
      uni.navigateBack({
        delta,
        success: () => {resolve('成功')},
        fail: () => {reject('失败')}
      })
    })
  }
  if(url != undefined){ //  跳转连接
    if(routerModeArr.indexOf(mode) == -1 && mode === undefined){
      mode = 'navigateTo'
    }else if(routerModeArr.indexOf(mode) == -1){
      console.error('请输入正确请求方式(mode参数传递错误）') 
    }
    mode = pageInfo(url).isTabBar ? 'reLaunch' : mode
    return new Promise(function (resolve, reject) {
      uni[mode]({
        url: '/' + url,
        success: () => {resolve('成功')},
        fail: () => {reject('失败')}
      })
    })
  }
  console.error('请写入跳转链接或正确的请求方式(mode参数传递错误）')
}
export const getOption = (link)=>{
  return option[link || pageInfo().nowPage]
}